package com.symaster.common.swing;

import com.symaster.common.event.Event;
import com.symaster.common.event.*;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowListener;

/**
 * 弹窗基类
 *
 * @author yinmiao
 * @version 2022-11-23 19:07
 */
public abstract class BaseDialog extends BaseListener implements TextLoader.TextChangeListener {

    private TextLoader textLoader;
    private boolean listenerInit = false;
    private JDialog dialog;

    public BaseDialog() {
        openListener();
    }

    public void setTextLoader(TextLoader textLoader) {
        this.textLoader = textLoader;
        textLoader.addTextChangeListener(this);
    }

    @Override
    public void allChange() {
        initText();
    }

    private void openListener() {
        EventManger.addListener(listenerKey(), new EventExecutor() {
            @Override
            public EventResult handler(Event event) {
                initPage();
                initText();
                if (!listenerInit) {
                    initListener();
                    listenerInit = true;
                }
                initDialog();
                dialog.setContentPane(rootContainer());
                dialog.setPreferredSize(pageSize());
                dialog.pack();
                SwingUtil.toCenter(dialog);
                initData(event.getData());
                dialog.setModal(true);
                dialog.setVisible(true);
                DefaultEventResult success = EventResult.success;
                success.setResultData(result());
                return success;
            }
        });
    }

    public void setIconImage(Image image) {
        initDialog();
        dialog.setIconImage(image);
    }

    public void dispose() {
        initDialog();
        dialog.dispose();
    }

    public void pack() {
        initDialog();
        dialog.pack();
    }

    public void setModal(boolean modal) {
        initDialog();
        dialog.setModal(modal);
    }

    public void setDefaultCloseOperation(int operation) {
        initDialog();
        dialog.setDefaultCloseOperation(operation);
    }

    public void addWindowListener(WindowListener l) {
        dialog.addWindowListener(l);
    }

    public void setTitle(String title) {
        initDialog();
        dialog.setTitle(title);
    }

    private void initDialog() {
        if (dialog == null) {
            dialog = new JDialog();
        }
    }

    /**
     * @return 返回监听Key, 当调用此Key时将会打开此弹窗
     */
    public abstract String listenerKey();

    /**
     * 在此处初始化页面, 每次显示页面之前会调用此方法
     */
    public abstract void initPage();

    /**
     * @return 返回此弹窗大小
     */
    public abstract Dimension pageSize();

    /**
     * 在此处初始化数据
     */
    public abstract void initData(Object... o);

    /**
     * @return 返回数据
     */
    public abstract Object result();

    /**
     * @return 返回根
     */
    public abstract Container rootContainer();

    /**
     * 在此处初始化事件, 首次显示页面之前会调用此方法
     */
    public abstract void initListener();

    /**
     * 此处设置所有组件文件
     */
    public abstract void initText();

    /**
     * 获取文本
     */
    public String getText(String key) {
        return textLoader.getText(key);
    }
}
